(function($) {
    $(document).ready(function(){
        var tempScrollTop, currentScrollTop = 0; 
        var margin = 1;
        if (typeof endless_on_scroll_margin != 'undefined') {
            margin = endless_on_scroll_margin;
        };
        $(window).scroll(function(){
            currentScrollTop = $(window).scrollTop(); 
            //User is scrolling down, not up.
            if (tempScrollTop < currentScrollTop ){
                if ($("a.endless_more").is(":visible") && 
                        $(document).height() - 
                        $(window).height() - 
                        currentScrollTop 
                        <= margin) {
                    $("a.endless_more").click();
                }
            }
            tempScrollTop = currentScrollTop; 
        });
    });
})(jQuery);
